// pages/coupon-list/coupon-list.js
var app = getApp();
import wxbarcode from 'wxbarcode';
Page({
  data: {
	tabs: ['未使用', '已核销', '已过期'],
    couponList: [],
    isLoading: false,
    noMoreData: false,
	couponCode:'',
	activeIndex:0,
	showBarcodeModal: false // 是否显示二维码弹窗
  },
  onLoad: function (options) {
	this.loaddata();
	  var self=this;	  
	  app.util.getUserInfo(function (response) {
		  console.log(response)
	  	self.setData({
			userInfo: response
	  	});
	  	app.mdInit(function (cfg,mine,area) {
			console.log(area)
	  		self.setData({
	  			cfg: cfg,
	  			mine:mine,
	  			area:area,
	  			main_loaded:true
	  		});
	  	});
	  });
  },
 // 页面数据加载
    loaddata() {
        const self = this;
		if (this.data.isLoading || this.data.noMoreData) return;
        //console.log("self.data.like "+self.data.like);
		// 根据activeIndex确定状态
		const statusMap = [1, 2, 3];
		const status = statusMap[this.data.activeIndex];
        app.util.request({
			url: 'Coupon/mylists',
			data: {
			   status: status,
			},
			success: (res) => {				
				var data = res.data.data;
				self.setData({
					now:data.now,
					couponList:data.coupon
				});
			  },
			  fail: (err) => {
				wx.showToast({
				  title: '加载失败',
				  icon: 'none'
				});
			  },
			  complete: () => {
				this.setData({ loading: false });
			  }
        })
    },
	// 切换tab
	  handleTabChange(e) {
	    const index = e.currentTarget.dataset.index;
	    if (this.data.activeIndex === index) return;
	    
	    this.setData({
	      activeIndex: index,
	      couponList: []
	    }, () => {
	      this.loaddata();
	    });
	  },
  // 核销操作
  handleVerify(e) {
	const self = this
    const id = e.currentTarget.dataset.id
	const hospitalid = e.currentTarget.dataset.hospitalid
	self.setData({
		couponCode:e.currentTarget.dataset.code
	})
	this.generateBarcode(e.currentTarget.dataset.code)
   //  wx.showModal({
   //    title: '确认核销',
   //    content: '确定要使用该优惠券吗？',
   //    success: res => {
   //      if (res.confirm) {			
			// wx.getLocation({
			// 	type: 'wgs84',
			// 	success(res) {
			// 		app.util.request({
			// 			url: 'Index/getDistance',
			// 			data: {
			// 				hospitalid: hospitalid,
			// 				lat:res.latitude,
			// 				lng:res.longitude
			// 			},
			// 			success(res2) {
			// 				console.log(res2)
			// 				if(res2.data.data){
			// 					self.updateCouponStatus(id)
			// 				}else{
			// 					wx.showToast({ title: res2.msg })
			// 				}
			// 			},
			// 			fail: (err) => {
			// 				console.log(res)
			// 				wx.showToast({
			// 				  title: '核销失败',
			// 				  icon: 'none'
			// 				});
			// 			},
			// 		})
			// 	},
			// 	fail(res) {
			// 		wx.showToast({ title: '定位失败' })
			// 	}
			// });
   //      }
   //    }
   //  })
  },
// 生成条形码
  generateBarcode(couponCode) {    

    // 生成条形码
    wxbarcode.qrcode('qrcode', couponCode, 600, 600);
    console.log(couponCode)
    // 显示弹窗
    this.setData({
      showBarcodeModal: true
    });
  },

  // 关闭弹窗
  closeModal() {
    this.setData({
      showBarcodeModal: false
    });
  },
  // 更新优惠券状态
  updateCouponStatus(id) {
    const newList = this.data.couponList.map(item => {
      if (item.id === id) {
        return { ...item, status: 2 }
      }
      return item
    })
	app.util.request({
	        url: 'Coupon/usecoupon',
	        data: {
						  id:id
	        },
	        success(res) {
	          wx.showToast({ title: '核销成功', icon: 'none' })			  
	        }
	})
    this.setData({ couponList: newList })
    wx.showToast({ title: '核销成功' })
  },
})